package com.fh.controller.building.room;

import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.fh.entity.system.TreeNode;
import com.fh.entity.system.User;
import com.fh.service.attachment.AttachmentService;
import com.fh.service.building.RecordService;
import com.fh.service.hydropower.ProjectHydopowerService;
import com.fh.util.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;

import com.fh.entity.Page;
import com.fh.service.building.room.RoomService;

import static com.fh.controller.system.tools.GetDatalayui.getLData;
import static com.fh.controller.system.tools.GetDatalayui.getPData;

/** 
 * 类名称：RoomController
 * 创建人：FH 
 * 创建时间：2017-09-11
 */
@Controller
@RequestMapping(value="/room")
public class RoomController extends BaseController {
	
	@Resource(name="roomService")
	private RoomService roomService;
	@Resource(name = "projectHydopowerService")
	private ProjectHydopowerService projectHydopowerService;
	@Resource(name = "attachmentService")
	private AttachmentService attachmentService;
	@Resource(name = "recordService")
	private RecordService recordService;

	/**
	 * 通过BUILDING_ID 和 FLOOR_ID获取列表
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public PageData list() {
		logBefore(logger, "room详情");
		PageData res = new PageData();
		PageData pd = this.getPageData();

		//获取当前登录用户
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user = (User) session.getAttribute(Const.SESSION_USER);

		try {
			pd.put("USER_ID", user.getUSER_ID());
			List<PageData> list = roomService.listAll(pd);

			res.put("data", list);
			res.put("errcode", "0");
			res.put("errmsg", "ok");
		} catch (Exception e) {
			logger.error(e.toString(), e);
			res.put("data", "");
			res.put("errcode", "1");
			res.put("errmsg", e.toString());
		}

		return res;
	}
	/**
	 * 新增
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public PageData save() {
		logBefore(logger, "新增room");
		PageData res = new PageData();
		PageData data = this.getPageData();

		//获取当前登录用户
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user = (User) session.getAttribute(Const.SESSION_USER);
		try {

			Map map1 = JSONHelper.json2Map(data.toString());
			Map map2 = JSONHelper.json2Map(map1.get("DATA").toString());
			JSONObject ITEM = (JSONObject) map2.get("ITEM");
			JSONArray ATTACHMENT = (JSONArray) map2.get("ATTACHMENT");
			String itemId = this.get32UUID();

			// 保存项目信息
			if (ITEM != null && !ITEM.equals("")) {
				PageData pd = new PageData();
				pd.put("ROOM_ID", itemId);    //主键
				pd.put("IS_DELETED", 0); //是否删除
				pd.put("SORT", 0); //排序
				pd.put("CREATE_ID", user.getUSER_ID()); //创建时间
				pd.put("CREATE_DATE", new DateUtil().getTime()); //创建时间
				pd.put("ROOM_NAME", ITEM.get("ROOM_NAME"));
				pd.put("ROOM_NUM", ITEM.get("ROOM_NUM"));
				pd.put("ROOM_AREA", ITEM.get("ROOM_AREA"));
				pd.put("ROOM_TYPE", ITEM.get("ROOM_TYPE"));
				pd.put("IS_OPEN", ITEM.get("IS_OPEN"));
				pd.put("ROOM_INFO", ITEM.get("ROOM_INFO"));
				pd.put("FLOOR_ID", ITEM.get("FLOOR_ID"));
				pd.put("UNIT_ID", ITEM.get("UNIT_ID"));
				pd.put("BUILDING_ID", ITEM.get("BUILDING_ID"));
				pd.put("BUSINESS_ID", ITEM.get("BUSINESS_ID"));
				pd.put("PROPERTY_RIGHT_TYPE", ITEM.get("PROPERTY_RIGHT_TYPE"));
				pd.put("PROPERTY_RIGHT_PERSON", ITEM.get("PROPERTY_RIGHT_PERSON"));
				pd.put("ROOM_PROPERTY", ITEM.get("ROOM_PROPERTY"));//项目属性 0 商用/1住宅
				roomService.save(pd);
				//保存全局记录
				PageData SystemPd = new PageData();
				SystemPd.put("ID", this.get32UUID());//主键
				SystemPd.put("IS_DEL", 0); //是否删除
				SystemPd.put("SORT", 0); //排序
				SystemPd.put("CREATE_ID", user.getUSER_ID()); //创建用户
				SystemPd.put("CREATE_DATE", new DateUtil().getTime()); //创建时间
				SystemPd.put("DETAILS","新增项目");
				SystemPd.put("OPERA_TIME",new DateUtil().getTime());
				SystemPd.put("OPERATOR",user.getNAME());
				SystemPd.put("IP",user.getIP());
				SystemPd.put("OP_PD",pd.toString());
				recordService.SaveSystem(SystemPd);
			}

			//  保存附件信息
			if (ATTACHMENT.size() > 0) {
				for (int i = 0; i < ATTACHMENT.size(); i++) {
					PageData pd = new PageData();
					JSONObject jo = (JSONObject) ATTACHMENT.get(i);
					pd.put("ID", this.get32UUID());    //主键
					pd.put("OID", itemId);
					pd.put("NAME", jo.get("name"));
					pd.put("SIZE", jo.get("size"));
					pd.put("EXT", jo.get("ext"));
					pd.put("PATH", jo.get("url"));
					pd.put("TYPE", "item");
					pd.put("SORT", i + 1);
					pd.put("REMARK", "");
					pd.put("CREATE_TIME", new DateUtil().getTime());
					pd.put("CREATE_ID", user.getUSER_ID());
					pd.put("IS_DELETED", 0);
					attachmentService.saveAttachment(pd);
				}
			}

			res.put("data", "");
			res.put("errcode", "0");
			res.put("errmsg", "ok");
		} catch (Exception e) {
			logger.error(e.toString(), e);
			res.put("data", "");
			res.put("errcode", "1");
			res.put("errmsg", e.toString());
		}
		return res;
	}
	/*
	/**
	 * 新增
	 */

	/*
	@RequestMapping(value = "/saveProject")
	@ResponseBody
	public PageData saveProject() {
		logBefore(logger, "新增Project");
		PageData res = new PageData();
		PageData pd = this.getPageData();

		//获取当前登录用户
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user = (User) session.getAttribute(Const.SESSION_USER);

		try {
			pd.put("ROOM_ID", this.get32UUID());    //主键
			pd.put("IS_DELETED", 0); //是否删除
			pd.put("SORT", 0); //排序
			pd.put("CREATE_ID", user.getUSER_ID()); //创建时间
			pd.put("CREATE_DATE", new DateUtil().getTime()); //创建时间
			pd.put("ROOM_TYPE",1);
			roomService.saveProject(pd);
			res.put("data", "");
			res.put("errcode", "0");
			res.put("errmsg", "ok");
		} catch (Exception e) {
			logger.error(e.toString(), e);
			res.put("data", "");
			res.put("errcode", "1");
			res.put("errmsg", e.toString());
		}

		return res;
	}*/
	/**
	 * 修改
	 */
	@RequestMapping(value = "/edit")
	@ResponseBody
	public PageData edit() {
		logBefore(logger, "修改room");
		PageData res = new PageData();
		PageData pd = this.getPageData();
		//获取当前登录用户
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user = (User) session.getAttribute(Const.SESSION_USER);

		try {
			roomService.edit(pd);
			//保存全局记录
			PageData SystemPd = new PageData();
			SystemPd.put("ID", this.get32UUID());//主键
			SystemPd.put("IS_DEL", 0); //是否删除
			SystemPd.put("SORT", 0); //排序
			SystemPd.put("CREATE_ID", user.getUSER_ID()); //创建用户
			SystemPd.put("CREATE_DATE", new DateUtil().getTime()); //创建时间
			SystemPd.put("DETAILS","删除项目");
			SystemPd.put("OPERA_TIME",new DateUtil().getTime());
			SystemPd.put("OPERATOR",user.getNAME());
			SystemPd.put("IP",user.getIP());
			SystemPd.put("OP_PD",pd.toString());
			recordService.SaveSystem(SystemPd);
			res.put("data", "");
			res.put("errcode", "0");
			res.put("errmsg", "ok");
		} catch (Exception e) {
			logger.error(e.toString(), e);
			res.put("data", "");
			res.put("errcode", "1");
			res.put("errmsg", e.toString());
		}

		return res;
	}

	/**
	 * 修改项目带图片
	 */
	@RequestMapping(value = "/editItem")
	@ResponseBody
	public PageData editItem() {
		logBefore(logger, "修改item带图片");
		PageData res = new PageData();
		PageData data = this.getPageData();

		//获取当前登录用户
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user = (User) session.getAttribute(Const.SESSION_USER);

		try {

			Map map1 = JSONHelper.json2Map(data.toString());
			Map map2 = JSONHelper.json2Map(map1.get("DATA").toString());
			JSONObject ITEM = (JSONObject) map2.get("ITEM");
			JSONArray ATTACHMENT = (JSONArray) map2.get("ATTACHMENT");
			String itemId = ITEM.getString("ROOM_ID");

			// 保存项目信息
			if (ITEM != null && !ITEM.equals("")) {
				PageData pd = new PageData();
				pd.put("ROOM_ID", itemId);    //主键
				pd.put("ROOM_NAME", ITEM.get("ROOM_NAME"));
				pd.put("ROOM_NUM", ITEM.get("ROOM_NUM"));
				pd.put("ROOM_AREA", ITEM.get("ROOM_AREA"));
				pd.put("ROOM_TYPE", ITEM.get("ROOM_TYPE"));
				pd.put("IS_OPEN", ITEM.get("IS_OPEN"));
				pd.put("ROOM_INFO", ITEM.get("ROOM_INFO"));
				pd.put("FLOOR_ID", ITEM.get("FLOOR_ID"));
				pd.put("UNIT_ID", ITEM.get("UNIT_ID"));
				pd.put("BUILDING_ID", ITEM.get("BUILDING_ID"));
				pd.put("BUSINESS_ID", ITEM.get("BUSINESS_ID"));
				pd.put("PROPERTY_RIGHT_TYPE", ITEM.get("PROPERTY_RIGHT_TYPE"));
				pd.put("PROPERTY_RIGHT_PERSON", ITEM.get("PROPERTY_RIGHT_PERSON"));
				pd.put("ROOM_PROPERTY", ITEM.get("ROOM_PROPERTY"));//项目属性 0 商用/1住宅
				roomService.edit(pd);
				//保存全局记录
				PageData SystemPd = new PageData();
				SystemPd.put("ID", this.get32UUID());//主键
				SystemPd.put("IS_DEL", 0); //是否删除
				SystemPd.put("SORT", 0); //排序
				SystemPd.put("CREATE_ID", user.getUSER_ID()); //创建用户
				SystemPd.put("CREATE_DATE", new DateUtil().getTime()); //创建时间
				SystemPd.put("DETAILS","修改项目");
				SystemPd.put("OPERA_TIME",new DateUtil().getTime());
				SystemPd.put("OPERATOR",user.getNAME());
				SystemPd.put("IP",user.getIP());
				SystemPd.put("OP_PD",pd.toString());
				recordService.SaveSystem(SystemPd);
			}


			PageData p = new PageData();
			p.put("ID", itemId);
			attachmentService.deleteAttachmentByOid(p);
			//  保存附件信息
			if (ATTACHMENT.size() > 0) {
				for (int i = 0; i < ATTACHMENT.size(); i++) {
					PageData pd = new PageData();
					JSONObject jo = (JSONObject) ATTACHMENT.get(i);
					pd.put("ID", this.get32UUID());    //主键
					pd.put("OID", itemId);
					pd.put("NAME", jo.get("name"));
					pd.put("SIZE", jo.get("size"));
					pd.put("EXT", jo.get("ext"));
					pd.put("PATH", jo.get("url"));
					pd.put("TYPE", "item");
					pd.put("SORT", i + 1);
					pd.put("REMARK", "");
					pd.put("CREATE_TIME", new DateUtil().getTime());
					pd.put("CREATE_ID", user.getUSER_ID());
					pd.put("IS_DELETED", 0);
					attachmentService.saveAttachment(pd);
				}
			}

			res.put("data", "");
			res.put("errcode", "0");
			res.put("errmsg", "ok");
		} catch (Exception e) {
			logger.error(e.toString(), e);
			res.put("data", "");
			res.put("errcode", "1");
			res.put("errmsg", e.toString());
		}

		return res;
	}

	/**
	 * 通过ID获取详情信息
	 */
	@RequestMapping(value = "/info")
	@ResponseBody
	public PageData info() {
		logBefore(logger, "Room详情");
		PageData res = new PageData();
		PageData pd = this.getPageData();

		try {
			PageData data = roomService.findById(pd);
			pd.put("id", pd.get("ROOM_ID"));
			List<PageData> attachmentData = attachmentService.findByOId(pd);
			if(data!=null){
				//获取根据房间单元 因为单元暂时固定 取房间首位 不取数据库
				res.put("unit",data.get("UNIT_NAME"));
				//获取房间
				res.put("roomnum",data.get("ROOM_NUM"));
			}
			res.put("data", data);
			res.put("attachmentData", attachmentData);
			res.put("errcode", "0");
			res.put("errmsg", "ok");
		} catch (Exception e) {
			logger.error(e.toString(), e);
			res.put("data", "");
			res.put("errcode", "1");
			res.put("errmsg", e.toString());
		}
		return res;
	}

	/**
	 * 通过ID数组获取水表
	 */
	@RequestMapping(value = "/findHydropowerWaterByIds")
	@ResponseBody
	public PageData findHydropowerWaterByIds() {
		logBefore(logger, "通过ID数组获取水表");
		PageData res = new PageData();
		PageData pd = this.getPageData();

		try {
			String DATA_IDS = pd.getString("PROJECTID");
			if(null != DATA_IDS && !"".equals(DATA_IDS)) {
				String ArrayDATA_IDS[] = DATA_IDS.split(",");
				List<PageData> data = roomService.findHydropowerWaterByIds(ArrayDATA_IDS);
				res.put("data", data);
			}
			res.put("errcode", "0");
			res.put("errmsg", "ok");
		} catch (Exception e) {
			logger.error(e.toString(), e);
			res.put("data", "");
			res.put("errcode", "1");
			res.put("errmsg", e.toString());
		}
		return res;
	}

	/**
	 * 通过ID数组获取电表
	 */
	@RequestMapping(value = "/findHydropowerElectricByIds")
	@ResponseBody
	public PageData findHydropowerElectricByIds() {
		logBefore(logger, "通过ID数组获取电表");
		PageData res = new PageData();
		PageData pd = this.getPageData();

		try {
			String DATA_IDS = pd.getString("PROJECTID");
			if(null != DATA_IDS && !"".equals(DATA_IDS)) {
				String ArrayDATA_IDS[] = DATA_IDS.split(",");
				List<PageData> data = roomService.findHydropowerElectricByIds(ArrayDATA_IDS);
				res.put("data", data);
			}
			res.put("errcode", "0");
			res.put("errmsg", "ok");
		} catch (Exception e) {
			logger.error(e.toString(), e);
			res.put("data", "");
			res.put("errcode", "1");
			res.put("errmsg", e.toString());
		}
		return res;
	}

    /**
    * 返回list 列表
    */
    @RequestMapping(value = "/json")
    @ResponseBody
    public Object json(Page page) {
    PageData pd = new PageData();
    Map<String, Object> map = new HashMap<String, Object>();
    List<PageData> varList;
        String datalayui = null;
		//获取当前登录用户
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user = (User) session.getAttribute(Const.SESSION_USER);
        try {
        	//测试分页 导出为layui的json数据
        	pd = this.getPageData();
			pd.put("USER_ID", user.getUSER_ID());
        	getPData(page, pd);//必填 2参
        	varList = roomService.list(page);    //列出Building列表
        	datalayui = getLData(page, varList);//必填 3参
        } catch (Exception e) {
        	logger.error(e.toString(), e);
        } finally {
        	logAfter(logger);
        }
       	 	return datalayui;
		}


	/**
	 * 去新增页面
	 */
	@RequestMapping(value="/goAdd")
	public ModelAndView goAdd(){
		logBefore(logger, "去新增Room页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			mv.setViewName("building/room/room_edit");
			mv.addObject("msg", "save");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}

	/**
	 * 去合同页面
	 */
	@RequestMapping(value="/goContract")
	public ModelAndView goContract(){
		logBefore(logger, "去新增Room页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			mv.setViewName("building/room/room_edit");
			mv.addObject("msg", "save");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/**
	 * 去修改页面
	 */
	@RequestMapping(value="/goEdit")
	public ModelAndView goEdit(){
		logBefore(logger, "去修改Room页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			pd = roomService.findById(pd);	//根据ID读取
			mv.setViewName("building/room/room_edit");
			mv.addObject("msg", "edit");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}


	/**
	 * 查询项目类型
	 */
	@RequestMapping(value = "/list_type")
	@ResponseBody
	public PageData type() {
		logBefore(logger, "type列表");
		PageData res = new PageData();
		PageData pd = this.getPageData();

		//获取当前登录用户
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user = (User) session.getAttribute(Const.SESSION_USER);

		try {
			pd.put("USER_ID", user.getUSER_ID());
			List<PageData> list = roomService.listType(pd);
			res.put("data", list);
			res.put("errcode", "0");
			res.put("errmsg", "ok");
		} catch (Exception e) {
			logger.error(e.toString(), e);
			res.put("data", "");
			res.put("errcode", "1");
			res.put("errmsg", e.toString());
		}
		return res;
	}


	/**
	 * 批量删除
	 */
	@RequestMapping(value="/deleteAll")
	@ResponseBody
	public Object deleteAll() {
		logBefore(logger, "批量删除Room");
		PageData pd = new PageData();		
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			pd = this.getPageData();
			List<PageData> pdList = new ArrayList<PageData>();
			String DATA_IDS = pd.getString("DATA_IDS");
			if(null != DATA_IDS && !"".equals(DATA_IDS)){
				String ArrayDATA_IDS[] = DATA_IDS.split(",");
				roomService.deleteAll(ArrayDATA_IDS);
				pd.put("msg", "ok");
			}else{
				pd.put("msg", "no");
			}
			pdList.add(pd);
			map.put("list", pdList);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			logAfter(logger);
		}
		return AppUtil.returnObject(pd, map);
	}
	
	/**
	 * 导出到excel
	 * @return
	 */
	@RequestMapping(value="/excel")
	public ModelAndView exportExcel(){
		logBefore(logger, "导出Room到excel");
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try{
			Map<String,Object> dataMap = new HashMap<String,Object>();
			List<String> titles = new ArrayList<String>();
			titles.add("房间号");	//1
			titles.add("楼层");	//2
			titles.add("楼宇id");	//3
			titles.add("房间面积");	//4
			titles.add("创建人id");	//5
			titles.add("创建时间");	//6
			titles.add("排序");	//7
			titles.add("是否删除");	//8
			dataMap.put("titles", titles);
			List<PageData> varOList = roomService.listAll(pd);
			List<PageData> varList = new ArrayList<PageData>();
			for(int i=0;i<varOList.size();i++){
				PageData vpd = new PageData();
				vpd.put("var1", varOList.get(i).getString("ROOM_NUM"));	//1
				vpd.put("var2", varOList.get(i).getString("FLOOR"));	//2
				vpd.put("var3", varOList.get(i).getString("T_ID"));	//3
				vpd.put("var4", varOList.get(i).getString("ROOM_AREA"));	//4
				vpd.put("var5", varOList.get(i).getString("CREATE_ID"));	//5
				vpd.put("var6", varOList.get(i).getString("CREATE_DATE"));	//6
				vpd.put("var7", varOList.get(i).getString("SORT"));	//7
				vpd.put("var8", varOList.get(i).getString("IS_DEL"));	//8
				varList.add(vpd);
			}
			dataMap.put("varList", varList);
			ObjectExcelView erv = new ObjectExcelView();
			mv = new ModelAndView(erv,dataMap);
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/* ===============================权限================================== */
	public Map<String, String> getHC(){
		Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
	}
	/* ===============================权限================================== */
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format,true));
	}

	/**
	 *关联水电表
	 * @return
	 */
	@RequestMapping(value = "/relatehydropower")
	@ResponseBody
	public Object relateHydropower() {
		logBefore(logger, "获取水电表信息");
		PageData pd = new PageData();
		PageData resultPd = new PageData();
		//获取当前登录用户
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user = (User) session.getAttribute(Const.SESSION_USER);
		try {
			pd = this.getPageData();
			ArrayList<String> news = new ArrayList<>();
			ArrayList<String> olds = new ArrayList<>();
			ArrayList<String> mins = new ArrayList<>();
			if(pd.containsKey("ids")){
				String ids = pd.getString("ids");
				String[] datas = ids.split(",");
				for(String codeId:datas){
					news.add(codeId);
					mins.add(codeId);
				}
			}
			if(pd.containsKey("oldData")){
				String ids = pd.getString("oldData");
				String[] datas = ids.split(",");
				for(String codeId:datas){
					olds.add(codeId);
				}
			}
			news.removeAll(olds);
			olds.removeAll(mins);
			if(olds.size()>0){
				for (String id:olds){
					PageData ed = new PageData();
					ed.put("codeId",id);
					ed.put("projectId",pd.getString("ROOMID"));
					projectHydopowerService.deleteByHydro(ed);
				}
			}
			if(news.size()>0){
				for (String id:news){
					PageData nd = new PageData();
					nd.put("PROJECTID",pd.getString("ROOMID"));
					nd.put("CODEID",id);
					nd.put("IS_TRUE",1);
					nd.put("CREATE_ID",this.getSessionUser().getUSER_ID());
					nd.put("CREATE_DATE",new DateUtil().getTime());
					nd.put("SORT",0);
					nd.put("IS_DEL",0);
					nd.put("ID",this.get32UUID());
					projectHydopowerService.save(nd);
				}
			}
//			if(pd.containsKey("itemArray")){
//				JSONArray arr = JSONArray.fromObject(pd.getString("itemArray"));
//				if(arr.size()>0){
//					for (int i=0;i<arr.size();i++){
//						JSONObject obj = (JSONObject) arr.get(i);
//						if(obj.has("id") && obj.getString("type").equals("1")){
//							PageData ed = new PageData();
//							ed.put("id",obj.getString("id"));
//							projectHydopowerService.delete(ed);
//						}else {
//							if(obj.has("isNew") && obj.getString("isNew").equals("1")){
//								PageData nd = new PageData();
//								nd.put("PROJECTID",pd.getString("ROOMID"));
//								nd.put("CODEID",obj.getString("codeId"));
//								nd.put("IS_TRUE",1);
//								nd.put("CREATE_ID",this.getSessionUser().getUSER_ID());
//								nd.put("CREATE_DATE",new DateUtil().getTime());
//								nd.put("SORT",i);
//								nd.put("IS_DEL",0);
//								nd.put("ID",this.get32UUID());
//								projectHydopowerService.save(nd);
//							}
//						}
//					}
//				}
//			}
			//保存全局记录
			PageData SystemPd = new PageData();
			SystemPd.put("ID", this.get32UUID());//主键
			SystemPd.put("IS_DEL", 0); //是否删除
			SystemPd.put("SORT", 0); //排序
			SystemPd.put("CREATE_ID", user.getUSER_ID()); //创建用户
			SystemPd.put("CREATE_DATE", new DateUtil().getTime()); //创建时间
			SystemPd.put("DETAILS","关联水电表");
			SystemPd.put("OPERA_TIME",new DateUtil().getTime());
			SystemPd.put("OPERATOR",user.getNAME());
			SystemPd.put("IP",user.getIP());
			SystemPd.put("OP_PD",pd.toString());
			recordService.SaveSystem(SystemPd);

			resultPd.put("data",pd);
			resultPd.put("errcode", "00");
			resultPd.put("errmsg", "获取成功");
		} catch (Exception e) {
			resultPd.put("errcode", "01");
			resultPd.put("errmsg", "获取失败");
			logger.error(e.toString(), e);
		}
		return resultPd;
	}

	@RequestMapping(value = "/findRoomByRoomId")
	@ResponseBody
	public Object findRoomByRoomId() {
		logBefore(logger, "获取水电表信息");
		PageData pd = new PageData();
		PageData resultPd = new PageData();
		try {
			pd = this.getPageData();
			PageData data = new PageData();
			data = roomService.findRoom(pd);
			List<PageData> list = roomService.findRoomHydropower(pd);
			data.put("list",list);
			resultPd.put("data",data);
			resultPd.put("errcode", "00");
			resultPd.put("errmsg", "获取成功");
		} catch (Exception e) {
			resultPd.put("errcode", "01");
			resultPd.put("errmsg", "获取失败");
			logger.error(e.toString(), e);
		}
		return resultPd;
	}

	@RequestMapping(value = "/findRoomHydroTree")
	@ResponseBody
	public Object findRoomHydroTree() {
		logBefore(logger, "获取水电表tree");
		PageData pd = new PageData();
		PageData resultPd = new PageData();
		try {
			pd = this.getPageData();
			PageData data = new PageData();
			List<PageData> list = roomService.findRoomHydropower(pd);
			pd.put("pid","0");
			List<PageData> hydros = roomService.findByPid(pd);
			List<TreeNode> nodeList = new ArrayList<TreeNode>();
			for(PageData parent:hydros){
				TreeNode node = roomService.recursiveTree(parent.getString("ID"),pd,list);
				nodeList.add(node);
			}
			resultPd.put("data",nodeList);
			resultPd.put("oldData",roomService.getOldDatas(list));
			resultPd.put("errcode", "00");
			resultPd.put("errmsg", "获取成功");
		} catch (Exception e) {
			resultPd.put("errcode", "01");
			resultPd.put("errmsg", "获取失败");
			logger.error(e.toString(), e);
		}
		return resultPd;
	}
/* ===============================app房间接口================================== */
	/**
	 * 通过BUILDING_ID 和 FLOOR_ID获取列表
	 */
	@RequestMapping(value = "/roomdetail")
	@ResponseBody
	public PageData roomdetail(HttpServletRequest request) {
		logBefore(logger, "room详情");
		PageData res = new PageData();
		PageData pd = this.getPageData();
		try {

			PageData ress = new PageData();
			PageData resss = new PageData();
			List<PageData> list = roomService.roomdetail(pd);
			List<PageData> list1 = roomService.roomdetail1(pd);
			//获取域名地址 用于组装图片全路径
			StringBuffer url = request.getRequestURL();
			String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append("/").toString();
			List<String> listdata=  new ArrayList<>();

				PageData att=new PageData();
				att.put("id",pd.get("ID"));
				List<PageData> attachmentData = attachmentService.findByOId(att);//查看附件
				if(attachmentData.size()>0){
					for(int k=0;k<attachmentData.size();k++){
						listdata.add(tempContextUrl+attachmentData.get(k).get("PATH"));//存放房间img 数组
					}
					//listimg.put("img",tempContextUrl+attachmentData.get(0).get("PATH"));
					resss.put("IMG",listdata);
				}

			/*resss.put("IMG"," ");*/
			ress.put("IMG",listdata);
			ress.put("CONLIST",list);
			ress.put("TRAD",list1.get(0).get("TRAD"));
			ress.put("CODE",list1.get(0).get("CODE"));
			ress.put("AREA",list1.get(0).get("AREA"));
			ress.put("STATE",list1.get(0).get("STATE"));
			res.put("data", ress);
			res.put("errcode", "0");
			res.put("errmsg", "ok");
		} catch (Exception e) {
			logger.error(e.toString(), e);
			res.put("data", "");
			res.put("errcode", "1");
			res.put("errmsg", e.toString());
		}
		return res;
	}
}
